<?php

namespace app\web\service;

use app\common\logic\OrderLogic;
use app\common\logic\WxAppLogic;
use app\common\model\commonModel;
use app\common\model\trCache;
use app\common\model\trCarType;
use app\common\model\trOrder;
use app\common\model\trResource;
use app\common\model\trSystemSet;
use app\common\model\trUser;
use app\common\model\trVehicleLine;
use app\common\util\ErrorCode;
use app\common\util\Tools;
use app\common\util\WxPay;

/**
 * Class Auth
 * @package app\web\service
 * 分享信息服务类
 */
class ShareService
{
    private $returnData;

    public function __construct($data)
    {
        $this->returnData = $data;
    }

    //获取分享信息
    public function getShareInfo($userId){
        $model = new trUser();
        $wxQrCode = $model->getValue('share_qr',['id' =>$userId]);
        if (!$wxQrCode){  //生成专属二维码
            $wxQrCodeUrl = WxAppLogic::createQRCode($userId);
            if ($wxQrCodeUrl){
                //保存
                $res = $model->doUpdate(['share_qr' => $wxQrCodeUrl['url']],['id' => $userId]);
                if(!$res){
                    $this->returnData = ErrorCode::$errorCode["fail"];
                }
                $wxQrCode = $wxQrCodeUrl['url'];
            }
        }
        //获取背景图
        $shareData = trSystemSet::getValue("wxAppShare");
        if (empty($shareData['backgroundPicture'])){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        $data["backgroundPictureUrl"] = trResource::getValue("url", ["id" => $shareData["backgroundPicture"]]);
        $data["wxQrCodeUrl"] = $wxQrCode;
        if(strstr($data["backgroundPictureUrl"],"http://")){
            $data["backgroundPictureUrl"] = str_ireplace('http://','https://',$data["backgroundPictureUrl"]);
        }
        if(strstr($data["wxQrCodeUrl"],"http://")){
            $data["wxQrCodeUrl"] = str_ireplace('http://','https://',$data["wxQrCodeUrl"]);
        }
        $this->returnData['data'] = $data;
        return $this->returnData;
    }
}